package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.modules.app.dao.UserCarDao;
import io.renren.modules.app.dao.UserDao;
import io.renren.modules.app.entity.UserCarEntity;
import io.renren.modules.app.service.UserCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Auther zxl
 * @Date 2018/12/16 20:43
 * @Description
 **/
@Service("UserCarService")
public class UserCarServiceImpl extends ServiceImpl<UserCarDao, UserCarEntity> implements UserCarService {

    @Autowired
    private UserDao userDao;

    @Override
    public UserCarEntity queryOneByUserId(Long userId) {
        UserCarEntity userCarEntity = new UserCarEntity();
        userCarEntity.setUserId(userId);

        return baseMapper.selectOne(userCarEntity);
    }

    @Override
    public boolean insertOne(UserCarEntity userCarEntity) {

        userCarEntity.setCreateTime(new Date());
        boolean b = false;
        b =baseMapper.insert(userCarEntity) > 0 ? true : false;
        if (b){
            userDao.updateUserState(1,userCarEntity.getUserId());
        }
        return b;
    }

    @Override
    public boolean update(UserCarEntity userCarEntity) {

        userCarEntity.setCreateTime(new Date());
        Integer res = baseMapper.update(userCarEntity,new EntityWrapper<UserCarEntity>()
                .where("user_id={0}",userCarEntity.getUserId()));

        return res>0;
    }
}
